package com.personal_workspace_java.service.impl;

import ch.qos.logback.core.util.StringUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.personal_workspace_java.common.PageResponse;
import com.personal_workspace_java.common.Query;
import com.personal_workspace_java.common.SuperUtil;
import com.personal_workspace_java.exception.CustomException;
import com.personal_workspace_java.mapper.TodoMapper;
import com.personal_workspace_java.model.dto.TodoDto;
import com.personal_workspace_java.model.en.Todo;
import com.personal_workspace_java.model.vo.TodoVo;
import com.personal_workspace_java.service.TodoService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author 19324
*/
@Service
public class TodoServiceImpl  extends ServiceImpl<TodoMapper, Todo>
        implements TodoService {

    @Resource
    private TodoMapper todoMapper;

    //1.新增Todo
    @Override
    public void create(TodoDto todoDto) {
        //1.获取参数
        Long id= todoDto.getId();
        Long userId= todoDto.getUserId();
        String todoJson= todoDto.getTodoJson();
        //2.判断是否存在
        QueryWrapper<Todo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id" ,id);
        queryWrapper.eq("user_id" ,userId);
        queryWrapper.eq("todo_json" ,todoJson);
        Todo todo = todoMapper.selectOne(queryWrapper);
        if (todo != null) {
            throw new CustomException("用户已存在");
        }
        //3.封装对象
        todo = new Todo();
        todo.setId(id);
        todo.setUserId(userId);
        todo.setTodoJson(todoJson);
        todoMapper.insert(todo);
    }


    //2.查询ByTodoId
    @Override
    public TodoVo queryByTodoId(TodoDto todoDto) {
        //1.获取参数
        Long todoId = todoDto.getId();
        if(todoId == null){
            throw new CustomException("查询参数不能为空");
        }
        //2.查询
        Todo todo = todoMapper.selectById(todoId);
        if(todo == null){
            throw new CustomException("用户不存在");
        }
        //3.封装对象
        return BeanUtil.copyProperties(todo, TodoVo.class);
    }

    //3.查询By其他字段
    @Override
    public TodoVo queryByOther(TodoDto todoDto) {
        //1.获取参数
        List<Query> queryList = todoDto.getQueryList();
            if(queryList == null){
                throw new CustomException("查询参数不能为空");
            }
        //2.查询
        Map<String, Object> queryMap = SuperUtil.listToMap(queryList);
        QueryWrapper<Todo> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(queryMap);
        Todo todo= todoMapper.selectOne(queryWrapper);
        if(todo == null){
            throw new CustomException("用户不存在");
        }
        //3.封装对象
        return BeanUtil.copyProperties(todo, TodoVo.class);
    }
    //4.查询所有
    @Override
    public List<TodoVo> queryAll() {
        //1.查询
        List<Todo> Todos = todoMapper.selectList(null);
        // 2. 封装对象
        return Todos.stream().map(Todo -> BeanUtil.copyProperties(Todo, TodoVo.class)).collect(Collectors.toList());
    }


    //5.删除ByTodoId
    @Override
    public void deleteByTodoId(TodoDto todoDto) {
        //1.获取参数
        Long todoId = todoDto.getId();
        if(todoId == null){
            throw new CustomException("删除参数不能为空");
        }
        //2.删除
        todoMapper.deleteById(todoId);
        if(todoMapper.selectById(todoId) != null){
            throw new CustomException("删除失败");
        }
    }


    //6.修改ByTodoId
    @Override
    public void updateByTodoId(TodoDto todoDto) {
        //1.获取ID
        Long todoId = todoDto.getId();
        Long userId = todoDto.getUserId();
        //2.获取参数
        if(todoId == null){
            throw new CustomException("修改参数不能为空");
        }
        //3.修改
        LambdaUpdateWrapper<Todo> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Todo::getId, todoId);
        lambdaUpdateWrapper.set(userId!=null,Todo::getUserId ,userId);
        todoMapper.update(null, lambdaUpdateWrapper);
    }

    @Override
    public PageResponse<TodoVo> queryTodoByPage(TodoDto todoDto) {
        //1.获取参数
        String sortBy = todoDto.getSortBy();
        Boolean isAsc = todoDto.getIsAsc();
        Page<Todo> page = todoDto.toPageDefaultSortByUpdateTime();
        if(StrUtil.isNotBlank(sortBy)&& isAsc != null){
            page = todoDto.toPage(sortBy, isAsc);
        }else if(StrUtil.isNotBlank(sortBy)){
            page = todoDto.toPageWithSortBy(sortBy);
        }else if(isAsc != null){
            page = todoDto.toPageWithIsAsc(isAsc);
        }

        List<Query> queryList = todoDto.getQueryList();
        List<String> serachList = todoDto.getSearchList();
        //2.查询
        QueryWrapper<Todo> queryWrapper = new QueryWrapper<>();
        if(queryList != null && !queryList.isEmpty()){
            Map<String, Object> queryMap = SuperUtil.listToMap(queryList);
            queryWrapper.allEq(queryMap);
        }

        // 处理搜索字段
        if (serachList != null && !serachList.isEmpty()) {
            String searchName = todoDto.getSearchName(); // 假设 todoDto 中有 searchValue 字段
            queryWrapper.and(wrapper -> {
                for (String field : serachList) {
                    wrapper.or().like(field, searchName);
                }
            });
        }
        List<Todo> todos = todoMapper.selectList(queryWrapper);
        //3.封装
        List<TodoVo> todoVos = todos.stream().map(todo -> BeanUtil.copyProperties(todo, TodoVo.class)).collect(Collectors.toList());
        return PageResponse.of(page,todoVos);
    }

}